#include <qyam3dview.h>
#include <graphics/utils.h>
#include <render/glextensions.h>

/*
QYam3DView::QYam3DView(QWidget* parent,
                       const QGLWidget* shareWidget, Qt::WindowFlags f) :
    QGLWidget(parent,shareWidget,f)
    , m_pfCreateScene(0)
    , m_scene(0)
    , m_initRequested(false)
    , m_clock()
{
}
*/

QYam3DView::QYam3DView(QWidget *parent, QGLWidget *shareWidget)
    : QGLWidget(parent, shareWidget)
    , m_pfCreateScene(0)
    , m_scene(0)
    , m_initRequested(false)
    , m_clock()
    , m_desiredFrameTime(1)
{
    setFocusPolicy(Qt::StrongFocus);
    setMouseTracking(true);
}

/*

QYam3DView::QYam3DView(QGLContext *context, QWidget* parent, const QGLWidget* shareWidget, Qt::WindowFlags f)
    : QGLWidget(context, parent, shareWidget, f)
    , m_pfCreateScene(0)
    , m_scene(0)
    , m_initRequested(false)
    , m_clock()
{
}

QYam3DView::QYam3DView(const QGLFormat& format, QWidget* parent, const QGLWidget* shareWidget, Qt::WindowFlags f)
    : QGLWidget(format, parent, shareWidget, f)
    , m_pfCreateScene(0)
    , m_scene(0)
    , m_initRequested(false)
    , m_clock()
{
}


*/


QYam3DView::~QYam3DView()
{
}

void QYam3DView::setSceneCreateFunc(SceneCreateFuncType pfCreateScene)
{
    m_pfCreateScene = pfCreateScene;

    if( m_initRequested )
    {
        init();
    }
}

/*
QSize QYam3DView::minimumSizeHint() const
{
    return QSize(50, 50);
}

QSize QYam3DView::sizeHint() const
{
    return QSize(400, 400);
}*/


void QYam3DView::resizeGL(int width, int height)
{
    if( m_scene != 0 )
    {
        m_scene->resize(width,height);
    }
}

void QYam3DView::initializeGL()
{
    m_initRequested = true;
    if( m_pfCreateScene != 0 )
    {
        init();
    }
}

void QYam3DView::update()
{
    float deltaTime= float(m_clock.restart())*0.001f;
    ++m_frameCount;
    if( m_Fpsclock.elapsed() > 5000 )
    {
        float time = 0.001f * float(m_Fpsclock.restart());
        qDebug( "Average FPS: %2.2f", float(m_frameCount)/float(time)  );
        m_frameCount=0;
    }

    if( m_scene != 0 )
    {
        m_scene->update(deltaTime);
    }

    updateGL(); // Update base class (this calls paintGL())

}

void QYam3DView::paintGL()
{
    // Clear background with blue. Clear Z-buffer.
    qglClearColor(Qt::black);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    if( m_scene != 0 )
    {
        m_scene->render();
    }
}

void QYam3DView::mousePressEvent(QMouseEvent *event)
{
    if( m_scene != 0 )
    {
        m_scene->mousePressEvent(event);
    }
}


void QYam3DView::mouseMoveEvent(QMouseEvent *event)
{
    if( m_scene != 0 )
    {
        m_scene->mouseMoveEvent(event);
    }
}

void QYam3DView::mouseReleaseEvent(QMouseEvent *event)
{
    if( m_scene != 0 )
    {
        m_scene->mouseReleaseEvent(event);
    }
}

void QYam3DView::keyPressEvent (QKeyEvent * event)
{
    QGLWidget::keyPressEvent(event);
    if( m_scene != 0 && !event->isAutoRepeat() )
    {
        m_scene->keyPressEvent(event);
    }
}

void QYam3DView::keyReleaseEvent (QKeyEvent * event)
{
    QGLWidget::keyReleaseEvent(event);
    if( m_scene != 0 && !event->isAutoRepeat() )
    {
        m_scene->keyReleaseEvent(event);
    }
}

void QYam3DView::init()
{
    assert( m_pfCreateScene != 0 );
    assert( m_scene == 0 );
    assert( m_initRequested == true );

    if (!getGLExtensionFunctions().resolve(context()))
    {
        QMessageBox::critical(0, "OpenGL features missing",
            "Failed to resolve OpenGL functions required to run this demo.\n" );
        return;
    }

    m_scene = m_pfCreateScene();
    m_scene->resize(width(),height());
    assert( m_scene != 0 );
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(update()));
    m_clock.start();
    m_Fpsclock.start();
    m_frameCount = 0;
    m_timer.start( m_desiredFrameTime );
}


